package com.ossean;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Scope;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import com.ossean.dao.GatherDao;
import com.ossean.model.OpenhubUrl;
import com.ossean.util.URLAvailability;

@Controller
@Scope("prototype")
public class HandleOpenhubPrj implements Runnable{

	@Qualifier("uRLAvailability")
	@Autowired
	private URLAvailability uRLAvailability;
	private int offset;
	private int limit;
	private String name;
	private static String targetTableName = TableName.gatherProjectsTableName;//去重项目的来源，来自汇总程序生成的表
	
	public static  ApplicationContext context=null;
	
	Logger logger = Logger.getLogger(this.getClass());
	
	public HandleOpenhubPrj(String name,int a,int b){
		this.name = name;
		this.offset = a;
		this.limit = b;
	}
	
	@Override
	public void run() {
		GatherDao gatherDao = (GatherDao) getInstance("gatherDao");
		//取完别名
		int count=0;
		long start =System.currentTimeMillis();
		List<OpenhubUrl> gpmList = (List<OpenhubUrl>) gatherDao.selectOpenhubPrj(targetTableName,"openhub",offset,limit);
		Thread t = Thread.currentThread();
		logger.info(name+" begin handle openhub project~");
		for(OpenhubUrl model:gpmList){
			count++;
			//logger.info(name+" : "+" have handle "+model.getUrl()+" projects~");
			if(count%1000 ==0)
				logger.info(name+" : "+" have handle "+count+" projects~");
			if(model.getUrl()==null)
				continue;
			if(!uRLAvailability.isRightUrlByRegex(model.getUrl())){
				logger.info(model.getId()+" url:"+model.getUrl());
				gatherDao.updateOpenhubUrl(targetTableName, null, model.getId());
				continue;
			}
			if(!uRLAvailability.isConnect2(model.getUrl())){
				logger.info(model.getId()+" url:"+model.getUrl());
				gatherDao.updateOpenhubUrl(targetTableName, null, model.getId());
			}
		}
		logger.info(name+"All openhub project have handled out!");
		logger.info(name+"stop!");
		
	}
	
	public static Object getInstance(String serviceName){

        if (null==context) {
            context=new ClassPathXmlApplicationContext("classpath:/applicationContext*.xml");;
        }

        return  context.getBean(serviceName);
    }
	

	public static void main(String [] args){
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:/applicationContext*.xml");
		new Thread(new HandleOpenhubPrj("thread1",6000,30000)).start();
		new Thread(new HandleOpenhubPrj("thread2",30000,30000)).start();
		new Thread(new HandleOpenhubPrj("thread3",60000,30000)).start();
		new Thread(new HandleOpenhubPrj("thread4",90000,30000)).start();
		new Thread(new HandleOpenhubPrj("thread5",120000,30000)).start();
		new Thread(new HandleOpenhubPrj("thread6",150000,30000)).start();
		new Thread(new HandleOpenhubPrj("thread7",180000,30000)).start();
		new Thread(new HandleOpenhubPrj("thread8",210000,30000)).start();
		new Thread(new HandleOpenhubPrj("thread9",240000,30000)).start();
		new Thread(new HandleOpenhubPrj("thread10",270000,30000)).start();
		new Thread(new HandleOpenhubPrj("thread11",300000,30000)).start();
		new Thread(new HandleOpenhubPrj("thread12",330000,30000)).start();
		new Thread(new HandleOpenhubPrj("thread13",360000,30000)).start();
		new Thread(new HandleOpenhubPrj("thread14",390000,30000)).start();
		new Thread(new HandleOpenhubPrj("thread15",420000,30000)).start();
		new Thread(new HandleOpenhubPrj("thread16",450000,30000)).start();
		new Thread(new HandleOpenhubPrj("thread17",480000,30000)).start();
		new Thread(new HandleOpenhubPrj("thread18",510000,30000)).start();
		new Thread(new HandleOpenhubPrj("thread19",540000,30000)).start();
		new Thread(new HandleOpenhubPrj("thread20",570000,30000)).start();
		new Thread(new HandleOpenhubPrj("thread21",600000,30000)).start();
	}
	

}
